﻿namespace Fiddler
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    public class QuickExec : TextBox
    {
        private string _sCueText;
        private bool bForceSave;
        private int iCurrentCommand;
        private List<string> slCommandHistory;

        public event ExecuteHandler OnExecute;

        public QuickExec()
        {
            base.AutoCompleteSource = AutoCompleteSource.CustomSource;
            base.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.slCommandHistory = new List<string>();
        }

        internal void LoadHistory(string sFile)
        {
            try
            {
                if (File.Exists(sFile))
                {
                    string[] collection = File.ReadAllLines(sFile, Encoding.UTF8);
                    this.slCommandHistory.AddRange(collection);
                    base.AutoCompleteCustomSource.AddRange(collection);
                    this.iCurrentCommand = this.slCommandHistory.Count;
                }
            }
            catch
            {
            }
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            this.UpdateCueText();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                    if (this.iCurrentCommand > 0)
                    {
                        this.iCurrentCommand--;
                        base.Clear();
                        if (this.iCurrentCommand < this.slCommandHistory.Count)
                        {
                            this.SelectedText = this.slCommandHistory[this.iCurrentCommand];
                        }
                    }
                    return true;

                case Keys.Down:
                    if (this.iCurrentCommand < this.slCommandHistory.Count)
                    {
                        this.iCurrentCommand++;
                        base.Clear();
                        if ((this.iCurrentCommand >= 0) && (this.iCurrentCommand < this.slCommandHistory.Count))
                        {
                            this.SelectedText = this.slCommandHistory[this.iCurrentCommand];
                        }
                    }
                    return true;

                case (Keys.Shift | Keys.Delete):
                    {
                        string sToDelete = this.Text;
                        this.slCommandHistory.RemoveAll(delegate(string s)
                        {
                            return s == sToDelete;
                        });
                        base.AutoCompleteCustomSource.Remove(sToDelete);
                        base.Clear();
                        this.iCurrentCommand = this.slCommandHistory.Count;
                        this.bForceSave = true;
                        return true;
                    }
                case Keys.Return:
                    {
                        string sCommand = this.Text.Trim();
                        if (sCommand.Length < 1)
                        {
                            base.Clear();
                            return true;
                        }
                        if ((this.OnExecute != null) && this.OnExecute(sCommand))
                        {
                            base.AutoCompleteCustomSource.Add(sCommand);
                            this.slCommandHistory.Add(sCommand);
                            this.iCurrentCommand = this.slCommandHistory.Count;
                        }
                        base.Clear();
                        return true;
                    }
                case Keys.Escape:
                    if (this.SelectedText.Length > 0)
                    {
                        this.SelectedText = string.Empty;
                    }
                    else
                    {
                        base.Clear();
                    }
                    return true;

                case (Keys.Control | Keys.Delete):
                    if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete ALL QuickExec command history?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                    {
                        base.AutoCompleteCustomSource.Clear();
                        this.slCommandHistory.Clear();
                        this.iCurrentCommand = 0;
                        this.bForceSave = true;
                    }
                    return true;

                case (Keys.Control | Keys.A):
                case (Keys.Control | Keys.Q):
                case (Keys.Alt | Keys.Q):
                    base.SelectAll();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        internal void SaveHistory(string sFile)
        {
            try
            {
                if (this.slCommandHistory.Count < 1)
                {
                    if (this.bForceSave)
                    {
                        File.Delete(sFile);
                    }
                }
                else
                {
                    List<string> list = new List<string>();
                    foreach (string str in this.slCommandHistory)
                    {
                        if (list.IndexOf(str) < 0)
                        {
                            list.Add(str);
                        }
                    }
                    File.WriteAllLines(sFile, list.ToArray(), Encoding.UTF8);
                }
            }
            catch
            {
            }
        }

        private void UpdateCueText()
        {
            if (base.IsHandleCreated)
            {
                Utilities.SetCueText(this, this.CueText);
            }
        }

        public string CueText
        {
            get
            {
                return this._sCueText;
            }
            set
            {
                this._sCueText = value;
                this.UpdateCueText();
            }
        }
    }
}

